/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmClientesBus.java
 *
 * Created on 02-sep-2011, 22:23:41
 */

package GUI;

import TablasAuxiliares.TATiposDocumento;
import Util.Varios;

/**
 *
 * @author Javi
 */
public class frmClientesBus extends javax.swing.JDialog {

    //Referencia al formulario padre
    private frmClientes _frmPadre;

    /** Creates new form frmClientesBus */
    public frmClientesBus(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /** Creates new form frmClientesBus */
    public frmClientesBus(frmClientes parent, boolean modal) {
        super(parent, modal);
        this._frmPadre = parent;
        initComponents();
        //Los combos se cargan a partir de las tablas 1 SOLA VEZ, al crear el formulario.
        this.cargarCombos();
        //Actualizo el título de la ventana
        this.setTitle( this._frmPadre.getoObjetoDatos().getsDescripTabla() + " - " + Varios.getLiteralOperacion('S') );
    }

    public void inicializaFormulario ( char cOperacion ){
        //El formulario se vacía cada vez que se entra en él
        this.inicializaControles();
        getRootPane().setDefaultButton(jButtonAceptar);
    }

    /*
     * Inicializa los combos a partir de las tablas auxiliares en forma "codigo - descripcion"
     */
    private void cargarCombos(){
        //Cargo el combo de Tipo de documento
        Varios.cargaCombo (jCBTipoDocumento, new TATiposDocumento());
    }

    /*
     * Este método inicializa los controles de la pantalla
     */
    private void inicializaControles (){
        this.jTFIdCliente.setValue(0);
        Varios.seleccionaItemCombo (jCBTipoDocumento, "");
        this.jTFNumDocumento.setText ("");
        this.jTFNombre.setText("");
        this.jTFApellidos.setText("");
    }

    /**
     * Este método forma la condición a aplicar a partir de los valores introducidos en la pantalla
     * @return
     */
    private String formaCondicionWhere(){
        String sCondicion = "";
        if ((Integer) (this.jTFIdCliente.getValue()) != 0)
            sCondicion += " IdCliente = " + (Integer) (this.jTFIdCliente.getValue()) + " AND ";
        if (this.jTFNombre.getText().compareTo("")!= 0)
            sCondicion += "Nombre LIKE '" + this.jTFNombre.getText() + "%' AND ";
        if (this.jTFApellidos.getText().compareTo("")!= 0)
            sCondicion += " Apellidos LIKE '" + this.jTFApellidos.getText() + "%' AND ";
        if (Varios.getClaveItemCombo (this.jCBTipoDocumento).compareTo("")!= 0)
            sCondicion += " TipoDocumento = '" + Varios.getClaveItemCombo (this.jCBTipoDocumento) + "' AND ";
        if (this.jTFNumDocumento.getText().compareTo("")!= 0)
            sCondicion += " NumDocumento LIKE '" + this.jTFNumDocumento.getText() + "%' AND ";

        //Quitar el último AND de la condición
        if (sCondicion.compareTo("") != 0)
            sCondicion = sCondicion.substring(0, sCondicion.lastIndexOf("AND") );
        return sCondicion;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel36 = new javax.swing.JLabel();
        jLabel38 = new javax.swing.JLabel();
        jCBTipoDocumento = new javax.swing.JComboBox();
        jLabel39 = new javax.swing.JLabel();
        jTFNumDocumento = new javax.swing.JTextField();
        jLabel40 = new javax.swing.JLabel();
        jTFNombre = new javax.swing.JTextField();
        jLabel41 = new javax.swing.JLabel();
        jTFApellidos = new javax.swing.JTextField();
        jButtonAceptar = new javax.swing.JButton();
        jButtonCancelar = new javax.swing.JButton();
        jTFIdCliente = new javax.swing.JFormattedTextField(new Integer(0));

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel36.setText("IdCliente");

        jLabel38.setText("Tipo Documento");

        jCBTipoDocumento.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel39.setText("Número de documento");

        jLabel40.setText("Nombre");

        jLabel41.setText("Apellidos");

        jButtonAceptar.setText("Aceptar");
        jButtonAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAceptarActionPerformed(evt);
            }
        });

        jButtonCancelar.setText("Cancelar");
        jButtonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelarActionPerformed(evt);
            }
        });

        jTFIdCliente.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel36)
                        .addGap(18, 18, 18)
                        .addComponent(jTFIdCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel40)
                        .addGap(15, 15, 15)
                        .addComponent(jTFNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(jLabel38)
                            .addGap(7, 7, 7)
                            .addComponent(jCBTipoDocumento, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel41)
                                .addGap(9, 9, 9)
                                .addComponent(jTFApellidos, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(282, 282, 282)
                                .addComponent(jLabel39)
                                .addGap(10, 10, 10)
                                .addComponent(jTFNumDocumento, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(189, Short.MAX_VALUE)
                .addComponent(jButtonAceptar)
                .addGap(18, 18, 18)
                .addComponent(jButtonCancelar)
                .addGap(168, 168, 168))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel36)
                    .addComponent(jTFIdCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel38)
                            .addComponent(jCBTipoDocumento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel40)
                            .addComponent(jTFNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel41)
                            .addComponent(jTFApellidos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jLabel39)
                    .addComponent(jTFNumDocumento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonAceptar)
                    .addComponent(jButtonCancelar))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAceptarActionPerformed
        //Actualizo la vista de los datos en el formulario padre con la condición especificada
        this._frmPadre.actualizarVista(this.formaCondicionWhere());
        //Ocultar la ventana solo si el resultado ha sido OK.
        setVisible(false);
}//GEN-LAST:event_jButtonAceptarActionPerformed

    private void jButtonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelarActionPerformed
        setVisible( false );
}//GEN-LAST:event_jButtonCancelarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAceptar;
    private javax.swing.JButton jButtonCancelar;
    private javax.swing.JComboBox jCBTipoDocumento;
    private javax.swing.JLabel jLabel36;
    private javax.swing.JLabel jLabel38;
    private javax.swing.JLabel jLabel39;
    private javax.swing.JLabel jLabel40;
    private javax.swing.JLabel jLabel41;
    private javax.swing.JTextField jTFApellidos;
    private javax.swing.JFormattedTextField jTFIdCliente;
    private javax.swing.JTextField jTFNombre;
    private javax.swing.JTextField jTFNumDocumento;
    // End of variables declaration//GEN-END:variables

}
